<template>
    <el-dialog :model-value="dialogVisible" :close-on-click-modal="true" :before-close="handleClose"
        :title="isAdd === 'addSub'? '添加子类' : isAdd === 'add' ? '新增业务子类' : '编辑业务子类'" append-to-body width="520px">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="父类" prop="parentId">
            <el-cascader style="width: 100%" ref="subBizRef" :disabled="isAdd !== 'add'" :show-all-levels="false" :options="tableData" v-model="form.parentId" :props="cacsProps" clearable @change="subBizChange" />
          </el-form-item>
          <el-form-item v-if="isAdd !== 'addSub'" label="业务类型" prop="bizTypeName">
              <el-select v-model="form.bizTypeName" :disabled="isAdd !== 'add' || form.parentId ? true : false" filterable
                  value-key="typeCode" placeholder="请选择业务类型" class="w100p">
                  <el-option v-for="item in typeOptions" :key="item" :label="item.typeName" :value="item.typeName" />
              </el-select>
          </el-form-item>
          <!-- <el-form-item label="父类名称">
              <el-input :disabled="true" v-model="form.parentName" :placeholder="isAdd === 'add' ? '新增默认第一级' : '父类名称'" />
          </el-form-item> -->
          <el-form-item label="业务子类名称" prop="subBizName">
              <el-input maxlength="20" v-model="form.subBizName" placeholder="请输入业务子类名称" />
          </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="handleClose">取消</el-button>
                <el-button :loading="loading" type="primary" @click="submit()">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script  setup>
import api from '@/api/index'
// eslint-disable-next-line no-unused-vars
import { statusText } from "@/util/index";
const props = defineProps({
  tableData: {
    type: Array,
    default: () => []
  },
  dialogVisible: {
      type: Boolean,
      default: false
  },
  isAdd: {
      type: String,
      default: 'add'
  },
  formData: {
      type: Object,
      default: () => { }
  },
  typeOptions: {
      type: Array,
      default: []
  }
})

watch(() => props.dialogVisible, (newVal) => {
    if (newVal === true) {
        if (props.isAdd === "add") {
            form.value = {}
        } else if (props.isAdd === "addSub") {
            form.value.parentId = props.formData.id
            form.value.parentName = props.formData.subBizName
            form.value.bizTypeName = props.formData.bizTypeName
        } else {
            form.value = { ...props.formData }
            form.value.bizTypeName = props.formData.bizTypeName
            oldName.value = props.formData.subBizName
        }
    }
})

const emit = defineEmits(['on-submit', 'handleClose'])
const formRef = ref(null)
const form = ref({})
const options = ref([])
const oldName = ref('')
const cacsProps = ref({
  value: 'id',
  label: 'subBizName',
  checkStrictly: true,
  emitPath: false
})
const rules = reactive({
    bizTypeName: [
        {
            // required: true,
            // message: '请选择业务类型',
            trigger: 'blur',
            validator: (rule, value, callback) => {
              console.log(form.value.parentId, 'form.value.parentId', value)
              if (!form.value.parentId && !value) {
                callback(new Error('业务类型和父类不能均为空！'))
              }
              callback()
            }
        }
    ],
    subBizName: [
        {
            required: true,
            message: '请输入业务子类名称',
            trigger: 'blur'
        },
        { max: 20, message: '业务子类名称最长为20个字符', trigger: 'blur' },
    ]
})
const loading = ref(false)

onMounted(() => {
})
const subBizRef = ref(null)
const subBizChange = (val) => {
  const tar = subBizRef.value.getCheckedNodes()
  if (val && tar && tar.length > 0) {
    form.value.bizTypeCode = tar[0].data.bizTypeCode
    form.value.bizTypeName = tar[0].data.bizTypeName
    form.value.codePath = tar[0].data.codePath
    form.value.namePath = tar[0].data.namePath
  } else {
    form.value.bizTypeCode = ''
    form.value.bizTypeName = ''
    form.value.codePath = ''
    form.value.namePath = ''
  }
  // form.value.bizTypeName = ''
  
}
const handleClose = () => {
    formRef.value.resetFields()
    emit('handleClose', false)
}
const submit = () => {
    if (!formRef) return
    formRef.value.validate(valid => {
        if (valid) {
            console.log(form.value)
            form.value.bizTypeCode = statusText(props.typeOptions, form.value.bizTypeName, 'typeCode', 'typeName')
            if (props.isAdd === 'edit') {
                if (oldName.value === form.value.subBizName) {
                    handleClose()
                } else {
                    emit('on-submit', { ...form.value })
                }
            } else {
                if (props.isAdd === 'add') {
                    // form.value.parentId = null
                }
                if (props.isAdd === 'addSub') {
                    form.value.id = null
                }
                emit('on-submit', { ...form.value })
            }

        } else {
            return false
        }
    })
}
</script>

<style lang="scss" scoped>
.dialog-footer {
    text-align: right;
}
</style>
